package com.sci99.ex.vm;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.zkoss.bind.BindContext;
import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.event.UploadEvent;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import com.sci99.ex.model.Category;
import com.sci99.ex.model.Course;
import com.sci99.ex.model.Question;
import com.sci99.ex.service.CategoryService;
import com.sci99.ex.service.CourseService;
import com.sci99.ex.service.QuestionService;
import com.sci99.ex.util.ExcelUtil;

public class QuestionVM {
	Logger log = Logger.getLogger(QuestionVM.class);
	private List<Question> list;
	private List<Category> cateList;
	private Category selectedCate;
	private List<Course> courList;
	private Course selectedCour;
	private List<Question> questionList;
	private Question selectedQues = new Question();

	public List<Question> getList() {
		return list;
	}

	public void setList(List<Question> list) {
		this.list = list;
	}

	public List<Category> getCateList() {
		return cateList;
	}

	public void setCateList(List<Category> cateList) {
		this.cateList = cateList;
	}

	public Category getSelectedCate() {
		return selectedCate;
	}

	public void setSelectedCate(Category selectedCate) {
		this.selectedCate = selectedCate;
	}

	public List<Course> getCourList() {
		return courList;
	}

	public void setCourList(List<Course> courList) {
		this.courList = courList;
	}

	public Course getSelectedCour() {
		return selectedCour;
	}

	public void setSelectedCour(Course selectedCour) {
		this.selectedCour = selectedCour;
	}

	public List<Question> getQuestionList() {
		return questionList;
	}

	public void setQuestionList(List<Question> questionList) {
		this.questionList = questionList;
	}

	public Question getSelectedQues() {
		return selectedQues;
	}

	public void setSelectedQues(Question selectedQues) {
		this.selectedQues = selectedQues;
	}

	@AfterCompose
	public void doAfterCompose() {
		try {
			cateList = CategoryService.getCategoryActiveList();
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}

	@Command("upload")
	@NotifyChange("list")
	public void upload(BindContext ctx) {
		UploadEvent event = (UploadEvent) ctx.getTriggerEvent();
		InputStream is = event.getMedia().getStreamData();
		try {
			list = ExcelUtil.importData(is);
		} catch (Exception e) {
			Messagebox.show("导入失败");
			log.error("", e);
		}
	}

	@Command
	@NotifyChange("list")
	public void saveImport() {
		if (selectedCate == null || selectedCour == null) {
			Messagebox.show("请选择课程");
			return;
		}
		if (list == null) {
			Messagebox.show("请先上传文件");
			return;
		}
		for (Question q : list) {
			q.setCourse_id(selectedCour.getId());
		}

		try {
			QuestionService.addQuestion(list);
		} catch (Exception e) {
			Messagebox.show(Msg.DbFail);
			log.error("", e);
			return;
		}
		Messagebox.show("成功导入" + list.size() + "条记录");
	}

	@Command
	@NotifyChange("courList")
	public void selCate() {
		Map<String, Object> args = new HashMap<>(2, 1);
		args.put("category_id", selectedCate.getId());
		try {
			courList = CourseService.getCourseList(args);
		} catch (Exception e) {
			Messagebox.show(Msg.DbFail);
			log.error("", e);
		}
	}

	@Command
	@NotifyChange("questionList")
	public void search() {
		if (selectedCate == null || selectedCour == null) {
			Messagebox.show("请选择课程");
			return;
		}
		try {
			questionList = QuestionService.getQuestionByCourseId(selectedCour
					.getId());
		} catch (Exception e) {
			Messagebox.show(Msg.DbFail);
			log.error("", e);
		}
	}

	@Command
	@NotifyChange("selectedQues")
	public void add(@BindingParam("win") Window edWin) {
		if (selectedCour == null) {
			Messagebox.show("请选择课程");
			return;
		}
		selectedQues = new Question();
		selectedQues.setCourse_id(selectedCour.getId());
		edWin.doModal();
	}

	@Command
	@NotifyChange("questionList")
	public void save(@BindingParam("win") Window edWin) {
		try {
			QuestionService.saveQuestion(selectedQues);
			questionList = QuestionService.getQuestionByCourseId(selectedCour
					.getId());
		} catch (Exception e) {
			Messagebox.show(Msg.DbFail);
			log.error("", e);
		}
		edWin.setVisible(false);
	}

	@Command
	@NotifyChange("questionList")
	public void del(@BindingParam("win") Window edWin) {

		if (Messagebox.show(Msg.CfmDel, null,
				Messagebox.OK + Messagebox.CANCEL, Messagebox.QUESTION) != Messagebox.OK)
			return;
		try {
			QuestionService.delQuetsion(selectedQues);
			questionList = QuestionService.getQuestionByCourseId(selectedCour
					.getId());
		} catch (Exception e) {
			Messagebox.show(Msg.DbFail);
			log.error("", e);
		}
		edWin.setVisible(false);
	}

	@Command
	public void close(@BindingParam("win") Window edWin) {
		edWin.setVisible(false);
	}

	@Command
	public void edit(@BindingParam("win") Window edWin) {
		edWin.doModal();
	}
}
